<template>
  <el-container class="home-container">

    <el-header>
      <!--头部标题-->
      <div>
        <router-link to="/" class="logo">WoniuLab</router-link>
      </div>
      <span class="title">个人管理</span>
      <div class="icon">
        <span>{{ currentUserName.username}}, 歡迎回来!</span>
        <el-dropdown>
          <el-avatar :src="currentSrc" size="50" ></el-avatar>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="returnHomePage">返回首页</el-dropdown-item>
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>

    <!--具体内容-->
    <el-container>
      <!--左侧菜单-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
<!--返回用戶中心主頁並收起側邊欄-->
        <div class="toggle-button" @click="toggleClapse" style="height: 50px;">收起側邊欄</div>
        <el-menu
            active-text-color="#ffd04b"
            router
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
        >
          <el-submenu v-for="item in menuList" :index="item.id + ''" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item
                v-for="subMenu in item.children"
                :index="subMenu.url+''"
                :key="subMenu.id"
            >
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{ subMenu.name }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧内容-->
      <el-main>
        <router-view v-if="isRouterAlive"/>
      </el-main>

    </el-container>


  </el-container>
</template>
<script>

import Cookies from "js-cookie";

export default {
  name: "UserCenter",
  data() {
    isDisplay:true
    return {
      //左侧菜单列表
      menuList: [
        {
          id: 1,
          name: "用户管理",
          children: [{id: 2, name: "个人中心", url: "/UserCenter"},
            {id: 3, name: "修改密碼", url: "/UpdatePass"}],

        },
        {
          id: 4,
          name: "历史记录",
          children: [{id: 5, name: "历史记录", url: "/History"}]
        },
        {
          id: 7,
          name: "个人收藏",
          children: [{id: 8, name: "个人收藏", url: "/Collection"}]
        },
      ],
      //左侧菜单是否收窄
      isCollapse: false,
      //右侧内容区路由是否激活
      isRouterAlive: true,
      currentUserName: '',
      currentSrc:'',
    };
  },
  methods: {
    toggleClapse() {
      this.isCollapse = !this.isCollapse;

    },
    initCurrentUser() {
      // let usertoken = sessionStorage.getItem("token")

      let usertoken  = Cookies.get('user');
      console.log("usertoken/////////////")
      console.log(usertoken)
      let config = {
        // params: {
          token: usertoken
        // }
      }
      console.log("config////////////////////////////////////////////")
      console.log(config)
      this.$axios.post("/userinfo/analysis", config)
          .then(response => {
            console.log(response)

            let responseData = response.data;
            console.log(responseData)

            if (responseData.code === 200) {
              let userdata = responseData.data;
              console.log( "userdata---------------------------------")
              console.log( userdata)

              if (userdata) {
                this.currentUserName = userdata;
                this.currentSrc = userdata.userIcon;
                console.log( "this.currentSrc-------------------------")
                console.log( this.currentSrc)
              }else {
                this.$message({
                  message: responseData.msg,
                  type: "error",
                  duration: 2000,
                })
                this.logout();
              }
            } else {
              this.$message({
                message: "登录状态异常,请重新登录",
                type: "error",
                duration: 2000,
              })
              this.logout();
            }
          })

    },
    returnHomePage() {
      sessionStorage.removeItem("token")
      location.href = "/"
    },
    logout() {
      this.$confirm('您确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Cookies.remove('user');
        this.loginStatus = '';
        this.$message({
          type: 'success',
          message: '已成功退出登录！'
        });
        this.$router.push('/');
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出登录'
        });
      });
    },

  },
  created() {
    this.initCurrentUser();

  }
};
</script>

<style lang="less" scoped>
img {
  width: 50px;
  height: 50px;
}

.toggle-button {
  background-color: #f8f8f8;
  font-size: 14px;
  text-align: center;
  color: #333;
  letter-spacing: 0.2em;
  cursor: pointer;
  height: 50px;
  line-height: 50px;
}

.el-header {
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #333;
  font-size: 22px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 14px;
    }
  }
}

.logo {
  font-size: 24px;
  font-weight: bold;
  text-decoration: none;
  cursor: pointer;
  margin-left: 45px;
  color: #409eff;
}

.title {
  font-style: italic;
  margin-left: 50px;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: black;
}

.icon {
  margin-left: 45px;
}

.el-aside {
  background-color: #f8f8f8;

  .el-menu {
    border-right: 0;
    background-color: #f8f8f8;
  }
}

.el-main {
  background-color: #eaedf1;
}

.home-container {
  height: 100%;
}

.el-menu:not(.el-menu--collapse) .el-menu-vertical-demo,
.el-menu-item,
.el-submenu .el-menu-item,
.el-submenu {
  background-color: #f8f8f8 !important;
}

.el-menu-item.is-active {
  background-color: #e6f1ff !important;
}

.el-menu .el-menu-item {
  color: #333 !important;
}

.el-menu .el-submenu__title {
  color: #333 !important;
  background-color: #f8f8f8 !important;
}

</style>


